package com.hh.goodsmanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.Check;
import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.date.DateFormat;
import com.hh.baseframework.util.dto.PageRange;
import com.hh.goodsmanage.bean.Goodsmanage;
import com.hh.goodsmanage.bean.GoodsmanageOut;
import com.hh.goodsmanage.util.Sql;
import com.hh.hibernate.util.dto.HQLParamList;
import com.hh.hibernate.util.dto.PagingData;
import com.hh.system.service.impl.BaseService;

@Service
public class GoodsmanageOutService extends BaseService<GoodsmanageOut> {
	@Autowired
	private GoodsmanageService goodsmanageService;

	@Override
	public PagingData<GoodsmanageOut> queryPagingData(GoodsmanageOut entity,
			PageRange pageRange) {
		PagingData<GoodsmanageOut> result = super.queryPagingData(entity,
				pageRange, new HQLParamList().addCondition(Restrictions.eq(
						"outBuyersId", entity.getOutBuyersId())));
		List<GoodsmanageOut> GoodsmanageOuts = result.getItems();
		for (GoodsmanageOut goodsmanageOut : GoodsmanageOuts) {
			if (Check.isNoEmpty(goodsmanageOut.getGoodsmanageId())) {
				Goodsmanage goodsmanage = goodsmanageService
						.findObjectById(goodsmanageOut.getGoodsmanageId());
				if (goodsmanage != null) {
					goodsmanageOut.setGoodsmanageId(goodsmanage.getName() + "（"
							+ goodsmanage.getCode() + "）");
				}

			}
		}
		return result;
	}

	public List<Map<String, Object>> monthChar(String year) {
		Map<String, String> paramsMap = new HashMap<String, String>();
		if (Check.isNoEmpty(year)) {
			paramsMap.put("year", year);
		} else {
			paramsMap.put("year", DateFormat.getYear());
		}

		List<Map<String, Object>> resultList = dao.queryListBySql(
				Sql.getMonthChar(), paramsMap);

		List<Map<String, Object>> resultMaps = new ArrayList<Map<String, Object>>();

		for (int i = 1; i <= 12; i++) {
			String istr = "" + i;
			if (istr.length() == 1) {
				istr = "0" + istr;
			}

			boolean as = true;

			for (Map<String, Object> map : resultList) {
				if (istr.equals(map.get("text"))) {
					resultMaps.add(map);
					as = false;
					break;
				}
			}
			if (as) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("text", istr);
				map.put("data", 0);
				resultMaps.add(map);
			}

		}

		return resultMaps;
	}

	public List<Map<String, Object>> findYear() {
		return dao.queryListBySql(Sql.getOutYear());
	}

	public Object dayChar(String year, String month) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("year", year);
		map.put("month", month);
		List<Map<String, Object>> resultList = dao.queryListBySql(
				Sql.getDayChar(), map);
		if (resultList.size() == 1) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("text", "==》");
			map2.put("data", 0);
			resultList.add(map2);
		}
		return resultList;
	}

	public Object yearChar(String year) {
		List<Map<String, Object>> resultList = dao.queryListBySql(
				Sql.getYearChar(), new HashMap<String, String>());
		if (resultList.size() == 1) {
			Map<String, Object> map2 = new HashMap<String, Object>();
			map2.put("text", "==》");
			map2.put("data", 0);
			resultList.add(map2);
		}
		return resultList;
	}
}